package com.zph.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebService;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.alibaba.fastjson.JSONObject;
import com.angel.base.utils.AngelObject;
import com.comments.tools.CVObj;
import com.comments.tools.TransAngelObj;
import com.comments.tools.TransCV;
import com.cvtech.pojo.ResumeInfo;
import com.webservice.Utils;
import com.zph.pojo.CVObject;

/**
 * 实时解析接口
 *
 */
/**
 * @DESC 简历解析web接口
 * @Author zhangzhenhua
 * @Date 2017年6月12日 下午2:53:20
 */
@WebService(serviceName = "ServiceStart")
public class ServiceStartImpl implements ServiceStart {
	private static String NAMESPACE = "http://tempuri.org/"; // http://chees.iok.la/
//	private static String URL = "http://chees.iok.la/ResumeService.asmx?WSDL";
	private static String URL = "http://114.112.106.126:6666/ResumeService.asmx?WSDL";
	
	/**
	 * 对外发布的解析简历文件的接口
	 * 
	 * @return
	 */
	@Override
	public String parseResume(DataHandler handler, String type) {
		// 转换
		AngelObject ao = new AngelObject();

		ResumeInfo resume = CvTech(handler, type);
		TransAngelObj.trans(resume, ao);
		return JSONObject.toJSONString(ao);
	}

	/**
	 * <p>
	 * 对外发布的解析简历文件的接口 -- 文件流上传
	 * </p>
	 * 
	 * @param handler
	 *            文件流
	 * @param pre
	 * @param type
	 * @param fileOriName
	 * @return
	 * @throws IOException
	 */
	@Override
	public String parseCv(DataHandler handler, String type) {

		// 转换
		CVObj cv = new CVObj();
		ResumeInfo resume = CvTech(handler, type);
		TransCV.trans(resume, cv);
		return JSONObject.toJSONString(cv);
	}


	/**
	 * 上传文件接口
	 * 
	 * @param handler
	 * @return
	 */
	@Override
	public String UploadFile(DataHandler handler, String pre, String type, String fileOriName) {
		return null;
	};

	/**
	 * 批量解析建立入口
	 */

	public CVObject PLparseCv(String filePathname) {
		return null;
	};

	public ResumeInfo CvTech(DataHandler handler, String type) {

		try {
			String username = "none"; // 用户名
			String pwd = "none"; // 密码
			String content = ""; // base64字符串
			String ext = type; // 文件扩展名，注意必须要加点号
			if (handler != null) {
				InputStream is = handler.getInputStream();
				byte[] b = Utils.getBytesFromInputStream(is); // 将文件转换成字节数组；
				content = Utils.getBase64String(b); // 将字节数组转换成base64字符串；

			} else {
				System.out.println("handler为空");
			}

			// 初始化SOAP对象
			SoapObject request = new SoapObject(NAMESPACE, "TransResumeByJsonStringForFileBase64"); // 调用方法名
			// 调用的参数
			request.addProperty("username", username);
			request.addProperty("pwd", pwd);
			request.addProperty("content", content);
			request.addProperty("ext", ext);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.bodyOut = request; // 带参数的话，一定要加这句；
			envelope.dotNet = true;

			envelope.setOutputSoapObject(request);

			// 调用SOAP
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			try {
				androidHttpTransport.call("http://tempuri.org/TransResumeByJsonStringForFileBase64", envelope);
			} catch (Exception e) {
				e.printStackTrace();
			}

			// 获得SOAP调用的结果
			SoapObject result = (SoapObject) envelope.bodyIn;

			if (result != null) {
				System.out.println(result.getProperty(0).toString());
				return JSONObject.parseObject(result.getProperty(0).toString(), ResumeInfo.class);

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}
